package elsa.controller;

import com.github.pagehelper.PageHelper;
import elsa.bean.ScsDoc;
import elsa.bootmapper.ConstMapper;
import elsa.bootmapper.DocMapper;
import elsa.bootmapper.OpsMapper;
import elsa.tools.Message;
import elsa.tools.Method;
import elsa.tools.MyResp;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
public class DocController {

    @Autowired
    private DocMapper docMapper;

    @Autowired
    private ConstMapper constMapper;

    @Autowired
    private OpsMapper opsMapper;

    //查询文档列表 兼 分页
    @CrossOrigin
    @RequestMapping(value={"/project/const/doc/{docnum}","/project/ops/doc/{docnum}"}, method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public MyResp docAllList(@PathVariable("docnum") String docnum,
                                @RequestParam("pageIndex") Integer pageIndex,
                                  @RequestParam("pageSize") Integer pageSize){
        if(docnum==null){
            docnum="";
        }
        if(pageIndex==null){
            pageIndex=1;
        }
        if(pageSize==null){
            pageSize=10;
        }
        PageHelper.startPage(pageIndex, pageSize);
        return  MyResp.render(0,"获取成功",docMapper.findAllDoc(docnum),docMapper.findDocCount(docnum));
    }

    //查询文档信息
    @CrossOrigin
    @RequestMapping(value={"/project/const/doc/info","/project/ops/doc/info"}, method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public ScsDoc docInfo(@RequestParam("docnum") String docnum, @RequestParam("uploadtime") String uploadtime){
        if(docnum==null){
            docnum="";
        }
        if(uploadtime==null){
            uploadtime="";
        }
        return  docMapper.findDocInfo(docnum, uploadtime);
    }

    //新建文档-船厂上传
    @CrossOrigin
    @RequestMapping(value = {"/project/const/doc/new", "/project/ops/doc/new"}, method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value = {"shipyard","admin","shipowner"},logical= Logical.OR)
    public Message addExp(@RequestBody ScsDoc scsDoc) {
        return new Message(setAddDoc(scsDoc));
    }

//    //更新文档-船厂上传
//    @CrossOrigin
//    @RequestMapping(value = {"/project/const/doc/updateCc", "/project/ops/doc/updateCc"}, method = RequestMethod.PUT)
//    @ResponseBody
//    @RequiresPermissions("admin")
//    public Message updateDocCc(@RequestBody ScsDoc scsDoc){
//        return new Message(setUpdateCcDoc(scsDoc));
//    }

    //更新文档-验船师审核
    @CrossOrigin
    @RequestMapping(value = {"/project/const/doc/updateYcs", "/project/ops/doc/updateYcs"}, method = RequestMethod.PUT)
    @ResponseBody
    @RequiresPermissions(value = {"surveyor","admin"},logical= Logical.OR)
    public Message updateDocYcs(@RequestBody ScsDoc scsDoc){
        return new Message(setUpdateYcsDoc(scsDoc));
    }

    //删除文档
    @CrossOrigin
    @RequestMapping(value = {"/project/const/doc/delete", "/project/ops/doc/delete"}, method = RequestMethod.DELETE)
    @ResponseBody
    @RequiresPermissions("admin")
    public Message deleteDoc(@RequestParam("docnum") String docnum, @RequestParam("uploadtime") String uploadtime){
        return new Message(setDeleteDoc(docnum, uploadtime));
    }

    //新建文档-船厂上传
    private String setAddDoc(ScsDoc scsDoc){
        String b;
        if(scsDoc.getDoc1()==null){
            scsDoc.setDoc1("");
        }
        if(scsDoc.getDoc2()==null){
            scsDoc.setDoc2("");
        }
        if(scsDoc.getDoc3()==null){
            scsDoc.setDoc3("");
        }
        if(scsDoc.getDoc4()==null){
            scsDoc.setDoc4("");
        }
        if(scsDoc.getDoc5()==null){
            scsDoc.setDoc5("");
        }
        if(scsDoc.getDoc6()==null){
            scsDoc.setDoc6("");
        }
        if(scsDoc.getDoc7()==null){
            scsDoc.setDoc7("");
        }
        if(scsDoc.getDoc8()==null){
            scsDoc.setDoc8("");
        }
        if (scsDoc.getDocnum()==null||(constMapper.findConstByInspnum(scsDoc.getDocnum()) == null && opsMapper.findOpsByRgstnum(scsDoc.getDocnum()) == null)) {
            b = "此项目不存在!";
        }else if(scsDoc.getDocname()==null||!chickName(scsDoc.getDocname())){
            b = "文档名称格式填写错误!";
        }else if(scsDoc.getDoc1().equals("")){
            b = "请上传文件";
        }else{
            Date date = new Date();
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String str = format.format(date);
            scsDoc.setUploadtime(str);
            scsDoc.setLatesttime(getNtime());
            scsDoc.setState("已上传");
            Method method = new Method();
            DateFormat dformat = new SimpleDateFormat("yyyyMMddHHmmss");
            String t = dformat.format(date);
            String path;
            if(constMapper.findConstByInspnum(scsDoc.getDocnum())!=null){
                path = "const/"+scsDoc.getDocnum()+"/constDoc"+t;
            }else{
                path = "ops/"+scsDoc.getDocnum()+"/opsDoc"+t;
            }
            if(!scsDoc.getDoc1().equals("")){
                method.copyFile(scsDoc.getDoc1(),path+scsDoc.getDoc1().substring(scsDoc.getDoc1().lastIndexOf("/")));
                scsDoc.setDoc1(path+scsDoc.getDoc1().substring(scsDoc.getDoc1().lastIndexOf("/")));
            }
            if(!scsDoc.getDoc2().equals("")){
                method.copyFile(scsDoc.getDoc2(),path+scsDoc.getDoc2().substring(scsDoc.getDoc2().lastIndexOf("/")));
                scsDoc.setDoc2(path+scsDoc.getDoc2().substring(scsDoc.getDoc2().lastIndexOf("/")));
            }
            if(!scsDoc.getDoc3().equals("")){
                method.copyFile(scsDoc.getDoc3(),path+scsDoc.getDoc3().substring(scsDoc.getDoc3().lastIndexOf("/")));
                scsDoc.setDoc3(path+scsDoc.getDoc3().substring(scsDoc.getDoc3().lastIndexOf("/")));
            }
            if(!scsDoc.getDoc4().equals("")){
                method.copyFile(scsDoc.getDoc4(),path+scsDoc.getDoc4().substring(scsDoc.getDoc4().lastIndexOf("/")));
                scsDoc.setDoc4(path+scsDoc.getDoc4().substring(scsDoc.getDoc4().lastIndexOf("/")));
            }
            if(!scsDoc.getDoc5().equals("")){
                method.copyFile(scsDoc.getDoc5(),path+scsDoc.getDoc5().substring(scsDoc.getDoc5().lastIndexOf("/")));
                scsDoc.setDoc5(path+scsDoc.getDoc5().substring(scsDoc.getDoc5().lastIndexOf("/")));
            }
            if(!scsDoc.getDoc6().equals("")){
                method.copyFile(scsDoc.getDoc6(),path+scsDoc.getDoc6().substring(scsDoc.getDoc6().lastIndexOf("/")));
                scsDoc.setDoc6(path+scsDoc.getDoc6().substring(scsDoc.getDoc6().lastIndexOf("/")));
            }
            if(!scsDoc.getDoc7().equals("")){
                method.copyFile(scsDoc.getDoc7(),path+scsDoc.getDoc7().substring(scsDoc.getDoc7().lastIndexOf("/")));
                scsDoc.setDoc7(path+scsDoc.getDoc7().substring(scsDoc.getDoc7().lastIndexOf("/")));
            }
            if(!scsDoc.getDoc8().equals("")){
                method.copyFile(scsDoc.getDoc8(),path+scsDoc.getDoc8().substring(scsDoc.getDoc8().lastIndexOf("/")));
                scsDoc.setDoc8(path+scsDoc.getDoc8().substring(scsDoc.getDoc8().lastIndexOf("/")));
            }
            docMapper.saveDoc(scsDoc);
            b = "文档新建成功!";
        }
        return b;
    }

//    //更新文档-船厂上传
//    private String setUpdateCcDoc(ScsDoc scsDoc) {
//        String b;
//        if(scsDoc.getDocnum()==null||scsDoc.getUploadtime()==null||(docMapper.findDocInfo(scsDoc.getDocnum(), scsDoc.getUploadtime())==null)){
//            b = "文档不存在!";
//        }else if(!chickName(scsDoc.getDocname())){
//            b = "文档名称格式填写错误!";
//        }else{
//            Date date = new Date();
//            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String str = format.format(date);
//            scsDoc.setAtime(str);
//            scsDoc.setLatesttime(getNtime());
//            Method method = new Method();
//            DateFormat dformat = new SimpleDateFormat("yyyyMMddHHmmss");
//            String t = dformat.format(date);
//            String path;
//            if(constMapper.findConstByInspnum(scsDoc.getDocnum())!=null){
//                path = "const/"+scsDoc.getDocnum()+"/constDoc"+t;
//            }else{
//                path = "ops/"+scsDoc.getDocnum()+"/opsDoc"+t;
//            }
//            if(!scsDoc.getDoc1().equals("")){
//                method.copyFile(scsDoc.getDoc1(),path+scsDoc.getDoc1().substring(scsDoc.getDoc1().lastIndexOf("/")));
//                scsDoc.setDoc1(path+scsDoc.getDoc1().substring(scsDoc.getDoc1().lastIndexOf("/")));
//            }
//            if(!scsDoc.getDoc2().equals("")){
//                method.copyFile(scsDoc.getDoc2(),path+scsDoc.getDoc2().substring(scsDoc.getDoc2().lastIndexOf("/")));
//                scsDoc.setDoc2(path+scsDoc.getDoc2().substring(scsDoc.getDoc2().lastIndexOf("/")));
//            }
//            if(!scsDoc.getDoc3().equals("")){
//                method.copyFile(scsDoc.getDoc3(),path+scsDoc.getDoc3().substring(scsDoc.getDoc3().lastIndexOf("/")));
//                scsDoc.setDoc3(path+scsDoc.getDoc3().substring(scsDoc.getDoc3().lastIndexOf("/")));
//            }
//            if(!scsDoc.getDoc4().equals("")){
//                method.copyFile(scsDoc.getDoc4(),path+scsDoc.getDoc4().substring(scsDoc.getDoc4().lastIndexOf("/")));
//                scsDoc.setDoc4(path+scsDoc.getDoc4().substring(scsDoc.getDoc4().lastIndexOf("/")));
//            }
//            if(!scsDoc.getDoc5().equals("")){
//                method.copyFile(scsDoc.getDoc5(),path+scsDoc.getDoc5().substring(scsDoc.getDoc5().lastIndexOf("/")));
//                scsDoc.setDoc5(path+scsDoc.getDoc5().substring(scsDoc.getDoc5().lastIndexOf("/")));
//            }
//            if(!scsDoc.getDoc6().equals("")){
//                method.copyFile(scsDoc.getDoc6(),path+scsDoc.getDoc6().substring(scsDoc.getDoc6().lastIndexOf("/")));
//                scsDoc.setDoc6(path+scsDoc.getDoc6().substring(scsDoc.getDoc6().lastIndexOf("/")));
//            }
//            if(!scsDoc.getDoc7().equals("")){
//                method.copyFile(scsDoc.getDoc7(),path+scsDoc.getDoc7().substring(scsDoc.getDoc7().lastIndexOf("/")));
//                scsDoc.setDoc7(path+scsDoc.getDoc7().substring(scsDoc.getDoc7().lastIndexOf("/")));
//            }
//            if(!scsDoc.getDoc8().equals("")){
//                method.copyFile(scsDoc.getDoc8(),path+scsDoc.getDoc8().substring(scsDoc.getDoc8().lastIndexOf("/")));
//                scsDoc.setDoc8(path+scsDoc.getDoc8().substring(scsDoc.getDoc8().lastIndexOf("/")));
//            }
//            docMapper.updateDocCc(scsDoc);
//            b = "文档更新成功!";
//        }
//        return b;
//    }

    //更新文档-验船师审核
    private String setUpdateYcsDoc(ScsDoc scsDoc) {
        String b;
        if(scsDoc.getDocnum()==null||scsDoc.getUploadtime()==null||docMapper.findDocInfo(scsDoc.getDocnum(), scsDoc.getUploadtime())==null){
            b = "文档不存在!";
        }else if(scsDoc.getYcschick()==null||scsDoc.getYcschick().length()==0){
            b = "请填写审核意见!";
        }else{
            scsDoc.setTxrtime(getNtime());
            scsDoc.setLatesttime(getNtime());
            scsDoc.setState("验船师已审核");
            docMapper.updateDocYcs(scsDoc);
            b = "文档更新成功!";
        }
        return b;
    }

    //删除文档
    private String setDeleteDoc(String docnum, String uploadtime){
        String b;
        if(docnum==null||uploadtime==null||docMapper.findDocInfo(docnum, uploadtime)==null){
            b = "该文档不存在!";
        }else {
            String path="";
            String t = uploadtime.replaceAll(":","").replaceAll("-","").replaceAll(" ","");
            if(constMapper.findConstByInspnum(docnum)!=null){
                path = "const/"+docnum+"/constDoc"+t;
            }else{
                path = "ops/"+docnum+"/opsDoc"+t;
            }
            String classPath = this.getClass().getClassLoader().getResource("").getPath();
            String thepath = classPath.substring(0,classPath.indexOf("WEB-INF"))+"file/";
            path = thepath+path;
            File dir = new File(path);
            System.out.println(path);
            Method method = new Method();
            System.out.println(method.deleteDir(dir));//删除所有文件
            docMapper.deleteDoc(docnum, uploadtime);
            b = "删除文档成功!";
        }
        return b;
    }


    /**
     * 其他操作
     */
    private boolean chickName(String name) {
        Pattern pattern = Pattern.compile("^[\u4E00-\u9FA5A-Za-z0-9]{1,20}$");
        Matcher match = pattern.matcher(name);
        return match.matches();
    }

    //获取当前时间
    private Date getNtime(){
        Date date = new Date();
        return new Timestamp(date.getTime());
    }

}
